package com.cn.hotel.dao.Impl;

import com.cn.hotel.dao.ICompanyDao;
import com.cn.hotel.model.AboutCompanyModel;
import com.cn.hotel.model.CompanyModel;
import com.cn.hotel.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CompanyImpl implements ICompanyDao {
    /**
     * 根据条件查询符合的所有的信息
     * @param companyModel
     * @return
     */
    @Override
    public List<CompanyModel> findAll(CompanyModel companyModel) {
        StringBuilder sql = new StringBuilder("select * from company where 1=1");
        if (companyModel.getCompanyName()!=null&&!"".equals(companyModel.getCompanyName())){
            sql.append(" and company_name like '%"+companyModel.getCompanyName()+"%'");
        }
        sql.append(" order by id asc limit " + (companyModel.getPageModel().getPageNum() - 1) * companyModel.getPageModel().getPageSize() + "" +
                "," + companyModel.getPageModel().getPageSize() + "");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        List<CompanyModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                CompanyModel model = new CompanyModel();
                model.setId(resultSet.getInt("id"));
                model.setCompanyName(resultSet.getString("company_name"));
                model.setLogo(resultSet.getString("logo"));
                model.setAd(resultSet.getString("ad"));
                model.setNet(resultSet.getString("net"));
                model.setIntroduce(resultSet.getString("introduce"));
                model.setVersion(resultSet.getString("version"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     *  获取数据库总数居
     * @param companyModel
     * @return
     */
    @Override
    public int getNumber(CompanyModel companyModel) {
        StringBuilder sql = new StringBuilder("select count(*) count from company where 1=1");
        if (companyModel.getCompanyName()!=null&&!"".equals(companyModel.getCompanyName())){
            sql.append(" and company_name like '%"+companyModel.getCompanyName()+"%'");
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        int count = 0;
        try{
            resultSet.next();
            count = resultSet.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public CompanyModel findById(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0) {
            sql.append("select * from company  where id =" + temp);
        }
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        CompanyModel model = new CompanyModel();
        try{
            while (resultSet.next()){
                model.setId(resultSet.getInt("id"));
                model.setCompanyName(resultSet.getString("company_name"));
                model.setLogo(resultSet.getString("logo"));
                model.setAd(resultSet.getString("ad"));
                model.setNet(resultSet.getString("net"));
                model.setIntroduce(resultSet.getString("introduce"));
                model.setVersion(resultSet.getString("version"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    /**
     * 保存修改
     * @param companyModel
     * @return
     */
    @Override
    public int edit(CompanyModel companyModel) {
        StringBuilder sql = new StringBuilder("update company set");
        if (!"".equals(companyModel.getCompanyName())){
            sql.append(" company_name = '"+companyModel.getCompanyName()+"',");
        }
        if (!"".equals(companyModel.getLogo())){
            sql.append(" logo = '"+companyModel.getLogo()+"',");
        }
        if (!"".equals(companyModel.getAd())){
            sql.append(" ad = '"+companyModel.getAd()+"',");
        }
        if (!"".equals(companyModel.getNet())){
            sql.append(" net = '"+companyModel.getNet()+"',");
        }
        if (!"".equals(companyModel.getIntroduce())){
            sql.append(" introduce = '"+companyModel.getIntroduce()+"',");
        }
        if (!"".equals(companyModel.getVersion())){
            sql.append(" version = '"+companyModel.getVersion()+"',");
        }
        sql.append(" remark = '' where id = "+companyModel.getId()+"");
        return JDBC.executeUpdate(sql.toString());
    }

    /**
     * 添加 新增
     * @param companyModel
     * @return
     */
    @Override
    public int add(CompanyModel companyModel) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into company(company_name,logo,ad,net,introduce,version) values('"+companyModel.getCompanyName()+"'," +
                "'"+companyModel.getLogo()+"','"+companyModel.getAd()+"'," +
                "'"+companyModel.getNet()+"','"+companyModel.getIntroduce()+"','"+companyModel.getVersion()+"')");
        return JDBC.executeUpdate(sql.toString());
    }

    @Override
    public int delete(Integer temp) {
        StringBuilder sql = new StringBuilder();
        if (temp!=0) {
            sql.append("delete  from company where id="+temp);
        }
        return JDBC.executeUpdate(sql.toString());
    }
//前端数据调取---------------------------------------------------

    /**
     * 获取关于公司的介绍
     * @return
     */
    @Override
    public AboutCompanyModel getAboutCompanyModel() {
        StringBuilder sql = new StringBuilder("select * from about_hotel");
        ResultSet resultSet = JDBC.getResultSet(sql.toString());
        AboutCompanyModel model = new AboutCompanyModel();
        try{
            while (resultSet.next()){
                model.setTitle(resultSet.getString("about_title"));
                model.setContent(resultSet.getString("about_content"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }
}
